package com.park.web.controller.pub;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.SearchText;
import com.park.client.model.app.AppVersionModel;
import com.park.client.model.article.ArticleCategoryModel;
import com.park.client.model.article.ArticleModel;
import com.park.client.model.article.ArticleTypeModel;
import com.park.client.model.book.BookModel;
import com.park.client.model.code.CodeModel;
import com.park.client.model.exam.ExamPaperTypeModel;
import com.park.client.model.moments.MomentsModel;
import com.park.client.model.pub.LinkModel;
import com.park.client.model.pub.VersionModel;
import com.park.client.model.question.QuestionModel;
import com.park.client.model.question.QuestionTypeModel;
import com.park.client.model.resource.ResourceFileModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.app.AppVersionService;
import com.park.client.service.article.ArticleCategoryService;
import com.park.client.service.article.ArticleService;
import com.park.client.service.article.ArticleTypeService;
import com.park.client.service.book.BookService;
import com.park.client.service.code.CodeService;
import com.park.client.service.exam.ExamPaperTypeService;
import com.park.client.service.moments.MomentsService;
import com.park.client.service.news.NewsService;
import com.park.client.service.pub.LinkService;
import com.park.client.service.pub.VersionService;
import com.park.client.service.question.QuestionService;
import com.park.client.service.question.QuestionTypeService;
import com.park.client.service.resource.ResouceFileService;
import com.park.client.service.user.UserService;
import com.park.client.vo.ArticleVo;
import com.park.client.vo.NewsVo;
import com.park.common.util.Page;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.enums.ArticleCategoryEnum;
import com.park.web.enums.BusinessTypeEnum;

@Controller
@RequestMapping("/public")
public class PublicIndexController extends BaseController{

	@Autowired
	private UserService userService;
	@Autowired
	private ArticleCategoryService categoryService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleTypeService articleTypeService;
	@Autowired
	private LinkService linkService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private AppVersionService appVersionService;
	@Autowired
	private VersionService versionService;
	@Autowired
	private MomentsService momentsService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private BookService bookService;
	@Autowired
	private CodeService codeService;
	@Autowired
	private ExamPaperTypeService examPaperTypeService;
	
	@RequestMapping("/index")
	public String index(HttpSession session,HttpServletRequest request){
		//后台开发
		List<ArticleModel> articleBackDev = articleService.selectByCategoryType(ArticleCategoryEnum.FIRST.categoryId);
		request.setAttribute("articleBackDev", articleBackDev);
		//数据库
		List<ArticleModel> articleDatabase = articleService.selectByCategoryType(ArticleCategoryEnum.SECOND.categoryId);
		request.setAttribute("articleDatabase", articleDatabase);
		//前端开发
		List<ArticleModel> articleFront = articleService.selectByCategoryType(ArticleCategoryEnum.THREE.categoryId);
		request.setAttribute("articleFront", articleFront);
		//服务器
		List<ArticleModel> articleServer = articleService.selectByCategoryType(ArticleCategoryEnum.FOUR.categoryId);
		request.setAttribute("articleServer", articleServer);
		//云计算
		List<ArticleModel> articleCloud = articleService.selectByCategoryType(ArticleCategoryEnum.FIVE.categoryId);
		request.setAttribute("articleCloud", articleCloud);
		//大数据
		List<ArticleModel> articleHadoop = articleService.selectByCategoryType(ArticleCategoryEnum.SEX.categoryId);
		request.setAttribute("articleHadoop", articleHadoop);
		//我的栏目
		List<ArticleTypeModel> articleTypeColList = articleTypeService.selectByColumn();
		request.setAttribute("articleTypeColList", articleTypeColList);
		
		//友情链接
		List<LinkModel> links = linkService.selectAll();
		request.setAttribute("links", links);
		
		//最新文章
		List<ArticleModel> articleList =  articleService.selectNewArticle();
		request.setAttribute("articleList", articleList);
		
		//评论最热文章
		List<ArticleModel> hotArticleList =  articleService.selectHotCommentArticle();
		request.setAttribute("hotArticleList", hotArticleList);
		
		//资讯
		List<NewsVo> newsList =  newsService.selectHotNews();
		request.setAttribute("newsList", newsList);
		
		//积分排行
		List<UserModel> userRanks = userService.selectIntegralRanking();
		request.setAttribute("userRanks", userRanks);
		
		//最新上传
		List<ResourceFileModel> resourceFiles = resouceFileService.selectRankingList();
		request.setAttribute("resourceFiles", resourceFiles);
		
		//首页最新评论文章
		List<ArticleVo> newArticels = articleService.selectNewArticleComment();
		request.setAttribute("newArticels", newArticels);
		
		//首页书籍显示
		List<BookModel> books =  bookService.selectByList();
		request.setAttribute("books", books);
		
		//随机推荐
		List<ArticleModel> recommenRecordList = articleService.selectRecommenRecord();
		request.setAttribute("recommenRecordList", recommenRecordList);
		
		return Global.PUBLIC_PATH+"index";
	}
	
	/**
	 * 文章大类列表
	 */
	@RequestMapping("/listCategory")
	@ResponseBody
	public Result<List<ArticleCategoryModel>> listCategory(HttpSession session){
		Result<List<ArticleCategoryModel>> result = new Result<>(true);
		try {
			result.setObj(categoryService.selectAll());
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
/*	*//**
	 * 后台开发
	 *//*
	@RequestMapping("/listArticleType")
	@ResponseBody
	public Result<List<Article>> listArticleType(HttpSession session){
		Result<List<Article>> result = new Result<>(true);
		try {
			List<String> list = new ArrayList<>();
			result.setObj(articleService.selectByType(list));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}*/
	
	/**
	 * 最新注册的用户
	 */
	@RequestMapping("/selectLastRegister")
	@ResponseBody
	public Result<List<UserModel>> selectLastRegister(HttpSession session){
		Result<List<UserModel>> result = new Result<>(true);
		try {
			result.setObj(userService.selectLastRegister());
			result.setBusinessType(BusinessTypeEnum.LAST_REGISTER.code);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	
	/**
	 * 交流问答
	 */
	@RequestMapping("interlocution")
	public String interlocution(Integer pageNow,HttpServletRequest request){
		QuestionModel question = new QuestionModel();
		Integer total = questionService.selectTotal(question);
		Page page = new Page(pageNow,total,10);
		question.setStartPos(page.getStartPos());
		question.setPageSize(page.getPageSize());
		 List<QuestionModel> questions = questionService.selectPagination(question);
		request.setAttribute("page", page);
		request.setAttribute("questions", questions);
		 List<QuestionTypeModel> questionTypes = questionTypeService.selectAll();
		 request.setAttribute("questionTypes", questionTypes);
		return Global.PUBLIC_PATH+"question/questionTypeList";
	}
	
	/**
	 * 版本更新
	 */
	@RequestMapping("version")
	public String version(HttpServletRequest request){
		//PC版本
		List<VersionModel> versions  = versionService.selectAll();
		request.setAttribute("versions", versions);
		//app版本
		AppVersionModel appVersion =  appVersionService.selectLatestVersion();
		request.setAttribute("appVersion", appVersion);
		Integer  totalDownCount = appVersionService.selectTotalDown();
		request.setAttribute("totalDownCount", totalDownCount);
		return Global.PUBLIC_PATH+"menu/version";
	}
	
	@RequestMapping("showMoments")
	@ResponseBody
	public Result<List<MomentsModel>> showMoments(){
		 Result<List<MomentsModel>> result = new Result<>(true);
		try {
			//动态
			List<MomentsModel> momentsList = momentsService.selectByLimit();
			result.setObj(momentsList);
		} catch (Exception e) {
			result.setSuccess(false);
		}
		return result;
	}
	
	/**
	 * 书籍
	 */
	@RequestMapping("bookList")
	public String bookList(@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,HttpServletRequest request){
		BookModel book = new BookModel();
		Integer total = bookService.selectTotal(book);
		Page page = new Page(pageNow,total,40);
		book.setStartPos(page.getStartPos());
		book.setPageSize(page.getPageSize());
		List<BookModel> bookList = bookService.selectPagination(book);
		request.setAttribute("bookList", bookList);
		request.setAttribute("page", page);
		return Global.PUBLIC_PATH+"book/bookList";
	}
	
	@RequestMapping("codeList")
	public String codeList(@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,HttpServletRequest request){
		CodeModel code = new CodeModel();
		Integer total = codeService.selectTotal(code);
		Page page = new Page(pageNow,total,10);
		code.setStartPos(page.getStartPos());
		code.setPageSize(page.getPageSize());
		List<CodeModel> codeList = codeService.selectPagination(code);
		request.setAttribute("codeList", codeList);
		request.setAttribute("page", page);
		return Global.PUBLIC_PATH+"code/codeList";
	}
	
	/**
	 * 搜索页面
	 */
	@RequestMapping("pearch")
	public String parkSearch(HttpServletRequest request){
		return Global.PUBLIC_PATH+"search/search";
	}
	
	/**
	 * 搜索结果页面
	 */
	@RequestMapping("searchList")
	public String searchList(String searchText,@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,HttpServletRequest request){
		SearchText tSearchText = new SearchText();
		tSearchText.setTitle(searchText.trim());
		Integer total = articleService.querySearchListTotal(tSearchText);
		Page page = new Page(pageNow,total,10);
		tSearchText.setStartPos(page.getStartPos());
		tSearchText.setPageSize(page.getPageSize());
		List<SearchText> searchTextList = this.articleService.querySearchList(tSearchText);
		request.setAttribute("searchTextList", searchTextList);
		request.setAttribute("page", page);
		return Global.PUBLIC_PATH+"search/searchList"; 
	}
	
	/**
	 * 个人信息
	 */
	@RequestMapping("userLand/{userId}")
	public String userLand(@PathVariable Integer userId,HttpServletRequest request){
		return Global.PUBLIC_PATH+"user/userLand";
	}
	
	/**
	 * 考试分类
	 */
	@RequestMapping("examTypeList")
	public String examTypeList(@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,HttpServletRequest request){
		ExamPaperTypeModel examPaperTypeModel = new ExamPaperTypeModel();
		examPaperTypeModel.setExamVisible(1);//可匿名参加的
		Integer total = examPaperTypeService.selectTotal(examPaperTypeModel);
		Page page = new Page(pageNow,total,10);
		examPaperTypeModel.setStartPos(page.getStartPos());
		examPaperTypeModel.setPageSize(page.getPageSize());
		List<ExamPaperTypeModel> examTypeList = examPaperTypeService.selectPagination(examPaperTypeModel);
		request.setAttribute("examTypeList", examTypeList);
		request.setAttribute("page", page);
		return Global.PUBLIC_PATH+"exam/examTypeList";
	}
	
}
